package com.chengqj.order.service;

import com.chengqj.order.domain.OrderDO;
import com.chengqj.order.repository.OrderRepository;
import com.chengqj.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Copyright © 2020年 tx-demo. All rights reserved.
 *
 * @author chengqj
 * 订单服务
 * @date 2020/11/13 17:35
 */
@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private UserService userService;

    public List<OrderDO> list(){
        return orderRepository.findAll();
    }
    @Transactional
    public void createOrder(Integer userId){
        OrderDO order = new OrderDO();
        order.setUserId(userId);
        order.setAmount(new BigDecimal(10));
        order.setGmtCreate(LocalDateTime.now());
        orderRepository.save(order);

        userService.updateAmount(userId);
        throw new RuntimeException("事务回滚测试异常");
    }
}
